package com.polito.mad.bikeponyapp.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.polito.mad.bikeponyapp.R;
import com.polito.mad.bikeponyapp.service.ServerCommunicationService;
import com.polito.mad.bikeponyapp.util.newTasksListAdapter;

public class NewTasksActivity extends Activity {

	private static final String RECEIVED_NEW_TASKS_PARAM_NAME = "receivedNewTasks";
	private Bundle receivedNewTasksBundle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_new_tasks);
		if(savedInstanceState!=null && savedInstanceState.containsKey(RECEIVED_NEW_TASKS_PARAM_NAME))
		{
			receivedNewTasksBundle = savedInstanceState.getBundle(RECEIVED_NEW_TASKS_PARAM_NAME);
			fillListAdapter();
		}
		else
		{
			Intent intent = getIntent();
			if (intent != null && intent.getAction() != null) {
	
				if (intent.getAction().equals(ServerCommunicationService.NEW_TASK_NOTIFICATION_ACTION)) {
					receivedNewTasksBundle = intent.getExtras();
					fillListAdapter();
				}
			}
		}
		getActionBar().setTitle(getString(R.string.new_task_activity_actionBar_title));
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	private void fillListAdapter() {
		
			List<String[]> newTasks = new ArrayList<String[]>();
			if(receivedNewTasksBundle != null)
			{
				Set<String> keys = receivedNewTasksBundle.keySet();
		
				for (String key : keys)
					newTasks.add(receivedNewTasksBundle.getStringArray(key));
			}
			ListView lv = (ListView) findViewById(R.id.newTasksList);
	
			lv.setAdapter(new newTasksListAdapter(this, newTasks));
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.new_tasks, menu);
		return false;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { // Respond to the action bar's Up/Home
									// button
		case android.R.id.home:
			Intent upIntent = NavUtils.getParentActivityIntent(this);
			if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
				// This activity is NOT part of this app's task, so create a new
				// task
				// when navigating up, with a synthesized back stack.
				TaskStackBuilder.create(this)
				// Add all of this activity's parents to the back stack
						.addNextIntentWithParentStack(upIntent)
						// Navigate up to the closest parent
						.startActivities();
			} else {
				// This activity is part of this app's task, so simply
				// navigate up to the logical parent activity.
				NavUtils.navigateUpTo(this, upIntent);
			}
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBundle(RECEIVED_NEW_TASKS_PARAM_NAME,receivedNewTasksBundle);
		super.onSaveInstanceState(outState);
	}

}
